//
//  MyAccountController.m
//  tuanGG
//
//  Created by 微他 on 15/9/22.
//  Copyright (c) 2015年 weita. All rights reserved.
//

#import "MyAccountController.h"
#import "Header.h"
#import "AddCodeViewController.h"
#import "MyAccountTableViewCell.h"
#import "ToApplyViewController.h"
#import "WithdrawalsViewController.h"
@interface MyAccountController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong,nonatomic)UITableView *tableView;
@property (strong,nonatomic)UIView *lineView ;
@property (strong,nonatomic)NSMutableArray *bankCodeArray;
@end

@implementation MyAccountController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self NetWorking];
    [self tabBarHidden];
}
- (void)viewWillDisappear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = UIColorFromRGB(base_gary_bg);
    [self addnavigationTitle:@"我的账户" navColor:@"4fd2be"];
    [self initlizeDataSource];
    [self initView];
}
- (void)initlizeDataSource {
    _bankCodeArray = [NSMutableArray array];
}
- (void)initView {
    UIView *oneView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, (ScreenHeight-64)/2)];
    oneView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:oneView];

    UILabel *balance = [self textLabel:[NSString stringWithFormat:@"%@元",[User_member instance].balance] frame:CGRectMake(0, 0, ScreenWidth-40, 30) location:1 textColor:[UIColor colorWithHexString:@"FF5500"] textFont:nil textFontNumber:30 backgroundColor:[UIColor clearColor]];
    balance.center = CGPointMake(ScreenWidth/2, oneView.frame.size.height/3-CGRectGetMaxY(balance.frame));

    UILabel *name = [self textLabel:@"账户余额" frame:CGRectMake(0, CGRectGetMaxY(balance.frame)+20, ScreenWidth, 20) location:1 textColor:[UIColor colorWithHexString:@"FF5500"] textFont:nil textFontNumber:15 backgroundColor:[UIColor clearColor]];


    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, oneView.frame.size.height/2, ScreenWidth, 0.5)];
    line.backgroundColor = [UIColor blackColor];
    line.alpha = 0.2;
    NSArray *array = @[@"添加银行卡",@"提现申请",@"提现记录"];
    for (int i = 0; i < 3; i++) {
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
        button.center = CGPointMake(button.frame.size.width+i*ScreenWidth/3, CGRectGetMaxY(line.frame)+button.frame.size.height);
        [button setImage:IMGNAME(array[i]) forState:UIControlStateNormal];
        button.tag =  i;
        [button addTarget:self  action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
        [oneView addSubview:button];

        UILabel *name = [self textLabel:array[i] frame:CGRectMake(i*ScreenWidth/3, CGRectGetMaxY(button.frame)+10, ScreenWidth/3, 20) location:1 textColor:[UIColor blackColor] textFont:nil textFontNumber:14 backgroundColor:[UIColor clearColor]];
        [oneView addSubview:name];
    }
    [oneView addSubview:line];
    [oneView addSubview:name];
    [oneView addSubview:balance];

    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, (ScreenHeight-64)/2+10, ScreenWidth, (ScreenHeight-64)/2-10)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UITableView alloc]init];
    [_tableView registerClass:[MyAccountTableViewCell class] forCellReuseIdentifier:@"CELL"];
    [self.view addSubview:_tableView];

    UILabel *headTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenHeight, 40)];
    headTitle.text= @"  我的银行卡信息";
    UIView *tableViewLine = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(headTitle.frame), ScreenWidth, 0.5)];
    tableViewLine.backgroundColor = [UIColor blackColor];
    tableViewLine.alpha = 0.2;
    [headTitle addSubview:tableViewLine];
    _tableView.tableHeaderView = headTitle;
}
#pragma -mark  事件实现
- (void)button:(UIButton *)sender {
    WithdrawalsViewController *WithdrawalsVC = [[WithdrawalsViewController alloc]init];
    ToApplyViewController *ToApplyVC = [[ToApplyViewController alloc]init];
    AddCodeViewController *AddCodeVC = [[AddCodeViewController alloc]init];
    switch (sender.tag) {
        case 0:[self.navigationController pushViewController:AddCodeVC animated:YES]; break;
        case 1:
            ToApplyVC.codeArray = [_bankCodeArray mutableCopy];
            [self.navigationController pushViewController:ToApplyVC animated:YES];
            break;
        case 2:[self.navigationController pushViewController:WithdrawalsVC animated:YES]; break;
        default:
            break;
    }
}
#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _bankCodeArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyAccountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSString *name = [NSString stringWithFormat:@"%@",_bankCodeArray[indexPath.row][@"bankname"]];
    [cell.image sd_setImageWithURL:nil placeholderImage:IMGNAME(_bankCodeArray[indexPath.row][@"bankname"])];
    cell.nameLabel.text = _bankCodeArray[indexPath.row][@"bankname"];
    NSString *string = [_bankCodeArray[indexPath.row][@"bankcount"] substringFromIndex:15];
    NSLog(@"%@",string);
    cell.codeDetails.text =[NSString stringWithFormat:@"尾号%@ 储蓄卡",string];
    return cell;
}
#pragma mark - 网络请求
- (void)NetWorking {
//    ToApplyViewController *toAppVC = [[ToApplyViewController alloc]init];
    NSDictionary *dictionary = nil;
    [GLAFRequest startRequest:count_url paramsDic:dictionary defaultdialog:YES ispost:NO handleSuccess:^(id sta, NSDictionary *paramsDic) {
        NSLog(@"银行卡信息：%@",sta);
        if ([sta[@"code"]integerValue] == 1) {
            _bankCodeArray = [sta[@"cardinfo"]mutableCopy];
            [_tableView reloadData];
        }
    } doFinally:^(NSDictionary *paramsDic) {

    }];
}
@end
